<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Document</title>
        <script src="../js/vue.js"></script>
    </head>
    <body>
        <div id="root">
            <h1>学生信息</h1>
            <button @click="student.age++">年龄+1岁</button> <br />
            <button @click="addSexType">添加性别属性，默认值男</button> <br />
            <button @click="student.sex='女'">修改性别</button> <br />
            <button @click="addFriend">在列表首位添加一个朋友</button> <br />
            <button @click="editFirstFriendName">修改第一个朋友的名字为张三</button> <br />
            <button @click="addHobby">添加一个爱好</button> <br />
            <button @click="editFirstHobby">修改第一个爱好为开车</button> <br />
            <button @click="filHobby">过滤掉爱好中的抽烟</button>
            <h3>姓名：{{student.name}}</h3>
            <h3>年龄：{{student.age}}</h3>
            <h3 v-if="student.sex">性别：{{student.sex}}</h3>
            <h3>爱好：</h3>
            <ul>
                <li v-for="h in student.hobby">{{h}}</li>
            </ul>
            <h3>朋友们：</h3>
            <ul>
                <li v-for="f in student.friend">{{f.name}}-{{f.age}}</li>
            </ul>
        </div>

        <script>
            Vue.config.productionTip = false

            const vm = new Vue({
                el:'#root',
                data:{
                    student:{
                        name:'tom',
                        age:18,
                        hobby:['抽烟','喝酒','烫头'],
                        friend:[
                            {name:'joe',age:19},
                            {name:'jonse',age:21}
                        ]
                    }
                },
                methods: {
                    addSexType(){ //添加性别属性 默认值：男
                        // Vue.set(this.student,'sex','男')
                        this.$set(this.student,'sex','男')
                    },
                    addFriend(){ //添加一个朋友
                        this.student.friend.unshift({name:'tony',age:'25'})
                    },
                    editFirstFriendName(){ //修改第一个朋友的名字为张三
                        // this.student.friend[0].name = '张三'
                        // Vue.set(this.student.friend[0],'name','张三')
                        this.$set(this.student.friend[0],'name','张三')
                    },
                    addHobby(){ //添加一个爱好
                        this.student.hobby.push("搓背")
                    },
                    editFirstHobby(){ //修改第一个爱好
                        // this.student.hobby.splice(0,1,'开车')
                        // Vue.set(this.student.hobby,0,'开车')
                        this.$set(this.student.hobby,0,'开车')
                    },
                    filHobby(){ //过滤掉爱好中的抽烟
                        this.student.hobby = this.student.hobby.filter((e) => {
                            return e != '抽烟'
                        })
                    }
                },
            })
        </script>
    </body>
</html>